<template>
  <el-dialog
    title="设置分组"
    :visible.sync="dialogVisible"
    width="500px"
    :show="show"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open='open'
  >
    <el-form ref="form" :model="info" label-width="90px" :rules="rules"
             v-loading="infoLoading">
      <div class="el-dialog-div form-scrollbar">
        <el-form-item label="选择分组" prop="group">
          <el-select
            v-model="info.group"
            class="filter-left"
            placeholder="请选择分组"
            clearable
            filterable
            size="small"
          >
            <el-option v-for="item in groupOptions" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">确定</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">取消</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {getGroupCollect} from "@/api/customer/group";
import {getCustomer, setGroupCollect} from "@/api/customer";
import {setBlockGroupCollect} from "@/api/customer/block";

export default {
  name: "Group",
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: '',
    closeAction: '',
    source: {
      type: String,
      default: "customer"
    },
  },
  data() {
    return {
      dialogVisible: this.show,
      infoLoading: true,
      groupOptions: [],
      info: {
        group: ''
      },
      rules: {
        //element form表单元素验证
        group: [{required: true, message: '请选择分组', trigger: 'blur'}],
      },
    }
  },
  methods: {
    handleClose(done) {
      this.$emit('update:show', false)
    },
    subInfo() {
      this.$refs['form'].validate(async (valid) => {
        if(valid){
          this.infoLoading = true
          let data = {
            group:this.info.group,
            ids:this.id
          }
          if (typeof data.ids != 'object') {
            data.ids = [data.ids]
          }
          if(this.source === 'customer'){
            await setGroupCollect(data).then((res) => {
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('closeAction')
            }).catch(() => {
              setTimeout(() => {
                this.infoLoading = false
              }, 0.5 * 1000)
            })
          }else if(this.source === 'block'){
            await setBlockGroupCollect(data).then((res) => {
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('closeAction')
            }).catch(() => {
              setTimeout(() => {
                this.infoLoading = false
              }, 0.5 * 1000)
            })
          }
        }
      })
    },
    async open() {
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      const {data} = await getGroupCollect()
      this.groupOptions = data
      if(typeof this.id != 'object'){
        let {data} = await getCustomer(this.id)
        this.info.group = data.groupId
      }else{
        this.info.group = ''
      }
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    }
  }
}
</script>

<style scoped>
.form-scrollbar {
  height: 200px !important;
}
</style>
